﻿using Microsoft.Xna.Framework;

namespace Ultima.Xna.Graphics.UI.Gumps
{
    public enum StackDirection
    {
        Vertical,
        Horizontal
    }

    public class GumpStackPanel : GumpBackground
    {
        StackDirection _stackDirection;
        float _itemSpacing;

        public float ItemSpacing
        {
            get { return _itemSpacing; }
            set { _itemSpacing = value; }
        }

        public StackDirection StackDirection
        {
            get { return _stackDirection; }
            set { _stackDirection = value; }
        }

        public GumpStackPanel(Game game)
            : base(game)
        {
            MovesParent = true;
        }

        protected override void OnChildAdded(object sender, NodeEventArgs e)
        {
            base.OnChildAdded(sender, e);

            e.Node.Dock = Dock.None;
            e.Node.Alignment = Alignment.None;

            float x = 0;
            float y = 0;

            float previousValue = 0;

            for(int i = 0; i < Children.Count; i++)
            {
                if (Children[i] != e.Node)
                {
                    switch (_stackDirection)
                    {
                        case StackDirection.Vertical:
                            y += Children[i].Position.Y + Children[i].Height - previousValue + ItemSpacing;
                            previousValue = y;
                            break;
                        case StackDirection.Horizontal:
                            x += Children[i].Position.X + Children[i].Width - previousValue + ItemSpacing;
                            previousValue = x + ItemSpacing;
                            break;
                    }
                }
            }

            e.Node.Position = new Vector2(x, y);
        }
    }
}
